package haizhi.fruitmall.service.impl;

import com.github.pagehelper.PageHelper;
import haizhi.fruitmall.dao.mapper.GoodsParamMapper;
import haizhi.fruitmall.dao.mapper.GoodsParamTypeMapper;
import haizhi.fruitmall.exception.ResultException;
import haizhi.fruitmall.pojo.form.GoodsParamForm;
import haizhi.fruitmall.pojo.po.GoodsParam;
import haizhi.fruitmall.pojo.po.GoodsParamExample;
import haizhi.fruitmall.service.GoodsParamService;
import haizhi.fruitmall.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by richa on 2017/9/13
 */

@Slf4j
@Service
@Transactional
public class GoodsParamServiceImpl implements GoodsParamService {

    @Autowired
    private GoodsParamMapper goodsParamMapper;
    
    @Autowired
    private GoodsParamTypeMapper goodsParamTypeMapper;

    @Override
    public void insert(GoodsParamForm form) {
        if(BeanUtil.isNull(goodsParamTypeMapper.selectByPrimaryKey(form.getTypeId())))throw new ResultException("记录类型不存在！");
        GoodsParam record = new GoodsParam();
        BeanUtil.copyBean(form,record);
        record.setTypeId(BeanUtil.intId());
        goodsParamMapper.insertSelective(record);
    }

    @Override
    public void update(GoodsParamForm form){
        GoodsParam record = this.getone(form.getParamId());
        if(BeanUtil.isNull(record)){ throw new ResultException("记录不存在！"); }
        BeanUtil.copyBean(form,record);
        record.setUpdateTime(BeanUtil.newTime());
        goodsParamMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void delete(Long id) {
        if(BeanUtil.isNull(this.getone(id))){ throw new ResultException("记录不存在！"); }
        goodsParamMapper.deleteByPrimaryKey(id);
    }

    @Override
    public GoodsParam getone(Long id) {
        if(BeanUtil.isNull(id)){ throw new ResultException("编号为空！");}
        return goodsParamMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<GoodsParam> getall(GoodsParamForm form) {
        GoodsParamExample example = new GoodsParamExample();
        GoodsParamExample.Criteria criteria = example.createCriteria();
        if(BeanUtil.noEmpty(form.getParamIdList())){
            criteria.andParamIdIn(form.getParamIdList());
        }
        if(BeanUtil.noNull(form.getPageNum()) && BeanUtil.noNull(form.getPageSize())){
            PageHelper.startPage(form.getPageNum(),form.getPageSize());
        }
        return goodsParamMapper.selectByExample(example);
    }
}
